//index.js
//获取应用实例
var httpRequest = require("../../utils/httpRequest.js")
const app = getApp()

Page({
  data: {
    motto: 'Hello World!@',
    userId: 123,
    show: false,
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    swiperData:[],
    // swiperImageWidth: wx.getSystemInfoSync().windowWidth,
    // swiperImageHeight: wx.getSystemInfoSync().windowWidth*500/1242,
    imgUrls: [
      {
        url: 'http://img.zcool.cn/community/017c5d554909920000019ae9d202fe.jpg@1280w_1l_2o_100sh.jpg'       },
      {
        url: 'http://5b0988e595225.cdn.sohucs.com/images/20170906/58cdb24be3624488ad3e8d3d00b4585f.jpeg'
      },
      {
        url: 'http://pic28.photophoto.cn/20130818/0020033143720852_b.jpg'
      },
      {
        url: 'http://img.zcool.cn/community/015da9554971170000019ae9f43459.jpg@2o.jpg'
      }
    ],
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../mainList/mainListPage'
    })
  },

  swipToMain:function(){
    wx.navigateTo({
      url: "../productList/productListPage"
    })
  },

  onLoad: function () {

    //获取轮播图接口
    var urlRequest = "http://ptit.azure-api.cn/v1/advert/promotion/list";
    var paramData = {};
    httpRequest.get(urlRequest, paramData).then(res => {
      console.log(res.data);
      this.setData({
        swiperData:res.data
      });
    }).catch(err => {
    });

    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }

  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  changeIndicatorDots: function (e) {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },
  changeAutoplay: function (e) {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },
  intervalChange: function (e) {
    this.setData({
      interval: e.detail.value
    })
  },
  durationChange: function (e) {
    this.setData({
      duration: e.detail.value
    })
  }
})
